﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CMS;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;

public partial class Page_view : System.Web.UI.Page
{
    public CMS.Functions fn = new CMS.Functions();
    public StoredProcedureValues Results = new StoredProcedureValues();

    private long? PageID = 0;
    private int Action;

    protected void Page_Init(object sender, EventArgs e)
    {
        if (CMS.Rights.PAG == false) { Response.Redirect("Forbidden.aspx"); }

        if (!CMS.Functions.VerifyLogin.Valid) { Response.Redirect("Login.aspx"); }
        else
        {
            if (Request.QueryString["PageID"] != null) { PageID = Convert.ToInt64(Request.QueryString["PageID"].ToString()); }            
            if (Request.QueryString["CMD"] != null) { Action = Convert.ToInt16(Request.QueryString["CMD"].ToString()); }

            Session.Add("PageID", PageID);
            Session.Add("CMD", Action);

            if (!IsPostBack) { FillValues(); }
        }
    }

    void FillValues()
    {
        if (Action == CMD.View || Action == CMD.Update || Action == CMD.Delete)
        {
            var Pages = CMS.Pages.GetPages(CMS.Functions.AdministrationID, PageID, null, null, null, null, null).FirstOrDefault();

            if (Pages != null && PageID.HasValue)
            {
                PAG_TITLE.Text = Pages.PAG_TITLE;
                PAGTitle.Text = " - " + Pages.PAG_TITLE;

                PAG_KEYWORDS.Text = Pages.PAG_KEYWORDS;
                PAG_METATAGS.Text = Pages.PAG_METATAGS;
                Content.Text = Pages.PAG_CONTENT;
                PAG_VISIBLE.Checked = Pages.PAG_VISIBLE.Value;
            }
        }

        if (Action == CMD.Delete)
        {
            DisableControls.DisableControl(this.Form);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if(Action == CMD.Update)
        {
              BtnSave.Text ="Edit";
        }
        else if(Action == CMD.Delete)
        {
              BtnSave.Text ="Delete";
        }
        

    }

    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Page.aspx");
    }
    
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {

        if (Action == CMD.Update)
        {
            Results = CMS.StoredProcedureValues.SP_Pages(CMD.Update, PageID, CMS.Functions.AdministrationID, PAG_TITLE.Text, Content.Text, PAG_KEYWORDS.Text, PAG_METATAGS.Text, PAG_VISIBLE.Checked);

            if (Results.Error == 0)
            {
                ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Updated succesfully !", "Page.aspx", System.Drawing.KnownColor.Black);
            }
            else
            {
                ((Content)Master).Jscript = CMS.Functions.NormalPopup(Results.Message, System.Drawing.KnownColor.Red);
            }

        }
       
        if (Action == CMD.Delete)
        {
            Results = CMS.StoredProcedureValues.SP_Pages(CMD.Delete, PageID, CMS.Functions.AdministrationID, null, null, null, null, null);

            if (Results.Error == 0)
            {
                ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Deleted succesfully !", "Page.aspx", System.Drawing.KnownColor.Black);
            }
            else
            {
                ((Content)Master).Jscript = CMS.Functions.NormalPopup(Results.Message, System.Drawing.KnownColor.Red);
            }

        }
       
        if (Action == CMD.Insert)
        {
            if (PAG_TITLE.Text != String.Empty && Content.Text != String.Empty)
            {
                Results = CMS.StoredProcedureValues.SP_Pages(CMD.Insert, null, CMS.Functions.AdministrationID, PAG_TITLE.Text, Content.Text, PAG_KEYWORDS.Text, PAG_METATAGS.Text, PAG_VISIBLE.Checked);

                if (Results.Error == 0)
                {
                    ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Inserted succesfully !", "Page.aspx", System.Drawing.KnownColor.Black);
                }
                else
                {
                    ((Content)Master).Jscript = CMS.Functions.NormalPopup(Results.Message, System.Drawing.KnownColor.Red);
                }

            }
            else
            {
                ((Content)Master).Jscript = CMS.Functions.NormalPopup("Please enter required fields with <em>*</em> !", System.Drawing.KnownColor.Black, " $('.Required').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');");
            }
        }
    }
}